package com.itfreer.arcgis.edit;

import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.mapping.view.SketchCreationMode;
import com.esri.arcgisruntime.mapping.view.SketchEditor;
import com.esri.arcgisruntime.mapping.view.SketchStyle;

/**
 * 绘制基础类
 */
public abstract class DrawTool
{
    protected SketchEditor sketchEditor;

    public DrawTool(SketchEditor sketchEditor)
    {
        EditToolManager.setCurrentTool(this);
        this.sketchEditor = sketchEditor;
    }

    /**
     * 绘制基础类,执行绘制方法
     * @param mapView
     * @param geometry
     */
    public void Draw(MapView mapView, Geometry geometry,DrawToolType drawToolType)
    {
        SketchStyle mainSketchStyle = new SketchStyle();
        sketchEditor.setSketchStyle(mainSketchStyle);
        mapView.setSketchEditor(sketchEditor);
        SketchCreationMode mode = getSketchMode(drawToolType);
        if(mode !=null)
        {
            if (geometry != null && !geometry.isEmpty())
                sketchEditor.start(geometry, mode);
            else
                sketchEditor.start(mode);
        }
    }

    /**
     * 清除绘制
     */
    public void Clear()
    {
        if(sketchEditor !=null)
            sketchEditor.stop();
    }

    /**
     * 获取绘制元素，在调用Clear 之前获取
     */
    public  Geometry GetGeometry()
    {
        if(sketchEditor!=null)
            return sketchEditor.getGeometry();

        return null;
    }

    public  void Back()
    {
        if(sketchEditor!=null && sketchEditor.canUndo())
            sketchEditor.undo();
    }

    public  void Forwad()
    {
        if(sketchEditor!=null && sketchEditor.canRedo())
            sketchEditor.redo();
    }

    private  SketchCreationMode getSketchMode(DrawToolType drawToolType)
    {
        switch (drawToolType)
        {
            case Point:
                return  SketchCreationMode.POINT;
            case Polyline:
                return  SketchCreationMode.POLYLINE;
            case Polygon:
                return  SketchCreationMode.POLYGON;
            case FreePolyline:
                return  SketchCreationMode.FREEHAND_LINE;
            case FreePolygon:
                return  SketchCreationMode.FREEHAND_POLYGON;
        }
        return  null;
    }
}
